//
//  Kylin_SettingTableViewController.m
//  01-LotteryBaseFrame
//
//  Created by Kylin on 16/1/8.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_SettingTableViewController.h"
#import "MBProgressHUD+XMG.h"
#import "Kylin_PushTableViewController.h"
@implementation Kylin_SettingTableViewController


- (instancetype)init{
    
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setUpGroup0];
   
    [self setUpGroup1];
   
    [self setUpGroup2];

    

}

- (void)setUpGroup0{
    
    
    // 定一个人组行模型
    NSArray *items = [NSArray array];
    
    Kylin_SettingArrowItem *item = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
     /**
     *  第一种方法实现跳转控制器
     */
//    item.desVc = [UIViewController class];
    
    
//    __weak Kylin_SettingTableViewController *weakSelf = self;
    
    // 把控制器的强指针 转换成弱指针
    __weak typeof(self) weakSelf = self;
    
    item.operationBlock = ^(NSIndexPath *indexPath){
        
        UIViewController *vc = [[UIViewController alloc]init];
        vc.title = @"设置1213";
        
        // 造成代码有一个强指针指向当前控制器,导致无法销毁
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    // 组模型加入每一组的模型
    items = @[item,];
    
    
    // 加入一个组模型 保存行模型,与头标题,和尾标题
    Kylin_SettingGroup *group = [Kylin_SettingGroup groupWithItems:items];
    
    group.headerText = @"abc";
    group.footerText = @"cdf";
    
    [self.groups addObject:group];
}

- (void)setUpGroup1{
    
    
    // 定一个人组行模型
    NSArray *items = [NSArray array];
    
    Kylin_SettingArrowItem *item11 = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"推送与提醒"];
//    item11.desVc = [Kylin_PushTableViewController class];
    
    __weak typeof(self) weakSelf = self;
    item11.operationBlock = ^(NSIndexPath *indexPath){
        Kylin_PushTableViewController *vc = [[Kylin_PushTableViewController alloc]init];
        
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    Kylin_SettingSwitchItem *item12 = [Kylin_SettingSwitchItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"使用兑换码"];
    item12.open = YES;
    
    Kylin_SettingSwitchItem *item13 = [Kylin_SettingSwitchItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"使用兑换码"];
    item13.open = YES;
    Kylin_SettingSwitchItem *item14 = [Kylin_SettingSwitchItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"使用兑换码"];
    // 组模型加入每一组的模型
    items = @[item11,item12,item13,item14];
    
    Kylin_SettingGroup *group = [Kylin_SettingGroup groupWithItems:items];
    group.headerText = @"yi";
    group.footerText = @"123";
    
    [self.groups addObject:group];
    
}

- (void)setUpGroup2{
    
    
    // 定一个人组行模型
    NSArray *items = [NSArray array];
    
    Kylin_SettingItem *item21 = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"handShake"]title: @"检查版本更新"];
    item21.operationBlock = ^(NSIndexPath *indexPath){
       
        [MBProgressHUD showSuccess:@"没有版本更新"];
    };
    Kylin_SettingItem *item22 = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"handShake"]title: @"使用兑换码"];
    
    Kylin_SettingItem *item23 = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"使用兑换码"];
    Kylin_SettingItem *item24 = [Kylin_SettingArrowItem itemWithImage:[UIImage imageNamed:@"lamp"]title: @"使用兑换码"];
    
    items = @[item21,item22,item23,item24];
    
    Kylin_SettingGroup *group = [Kylin_SettingGroup groupWithItems:items];
    group.headerText = @"kylin";
    group.footerText = @"155";
    
    // 组模型加入每一组的模型
    
    [self.groups addObject:group];
    
}
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.groups.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    
 
   return group.items.count;

    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Kylin_SettingTableViewCell *cell = [Kylin_SettingTableViewCell cellViewTableView:tableView];
    
    Kylin_SettingGroup *group = self.groups[indexPath.section];
    
    Kylin_SettingItem *item = group.items[indexPath.row];
    
    cell.item = item;
    
    return cell;
   
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    
    return group.headerText;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    return group.footerText;
}


// 跳转控制器 ,选中哪一行时会调用该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 取消选择
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 获取选中了 哪一行
    Kylin_SettingGroup *group = self.groups[indexPath.section];
    Kylin_SettingItem *item = group.items[indexPath.row];
    
    if (item.operationBlock) {
        item.operationBlock();
        
        // 使用多一次方法来实现跳转控制器
    }else if([item isKindOfClass:[Kylin_SettingArrowItem class]]){
        Kylin_SettingArrowItem *arrowItem = (Kylin_SettingArrowItem *)item;
        
        if (arrowItem.desVc) {
            UIViewController *vc = [[arrowItem.desVc alloc]init];
            
            vc.navigationItem.title = arrowItem.title;;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}
 */
@end
